
import { AIPanel } from "../AIPanel";

export class AITask {
    //目标资产
    genAsset:IEditor.IAssetInfo=null;

    /** 等待交互完成的函数，一旦调用就会释放promise的阻塞 */
    private waitItResolve:(value:unknown)=>void=null;
    /** 界面对应的msgid */
    protected msgid:string=null;
    _isCancel=false;

    onCancel(){}    //子类实现
    /**
     * 中断任务
     */
    cancel(silent=false){
        this.onCancel();
        if(!silent){
            //this.setui('任务被取消');
            this.deleteUI();   
        }
        else 
            this.deleteUI();

        this._isCancel=true;
        this.waitItEnd(false);
    }

    private deleteUI(){
        AIPanel.ins.removeMsg(this.msgid);
    }

    
    protected async setui(msg:string,option?:any){
        if(!this.msgid){
            this.msgid= await AIPanel.ins.addMsg(msg,-1,option);
        }else{
            AIPanel.ins.updateMsg(msg,this.msgid);
        }
    }    

    /**
     * 等待用户交互。一旦调用就会阻塞，直到用户交互完成，调用
     * @returns 
     */
    async waitIt(){
        return new Promise<any>((resolve)=>{
            this.waitItResolve = resolve;
        });
    }

    waitItEnd(confirmed:boolean){
        if(this.waitItResolve){
            this.waitItResolve(confirmed);
            this.waitItResolve = null;
        }
    }

    renderUI(){}

    /**
     * 确认能执行了，执行。
     */
    async execute():Promise<any>{
    }

    onSelectionChanged(){}

    //任务执行过程。是一个不断累加的字符串
    taskProcess:string;
}
